package com.wzc.user.controller;

import com.wzc.basic.annotation.PreAuthorize;
import com.wzc.basic.query.BaseQuery;
import com.wzc.basic.util.AjaxResult;
import com.wzc.basic.util.PageList;
import com.wzc.user.domain.User;
import com.wzc.user.dto.RegisterDto;
import com.wzc.user.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
@Api(value = "用户接口类",description = "用户相关CRUD")
public class UserController {


    @Autowired
    private IUserService userService;


    /**
     * 查询所有
     * @GetMapping
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询所有")
//    @PreAuthorize(name = "用户管理查询所有", sn = "user:list")
    public List<User> loadAll(){
        return userService.loadAll();
    }


    /**
     * 查询单个
     * @GetMapping("/{id}") 后端接收前端传输的数据（动态传输）
     * @PathVariable("id") 从路径上去获取id变量（后端取数据）再赋值给 Long id
     * @param id
     * @return
     */
    @GetMapping("/{id}")  // 接收参数 @PathVariable获取参数
    @ApiOperation(value = "通过id查询单个数据")
//    @PreAuthorize(name = "用户管理通过id查询单个数据", sn = "user:loadById")
    public User loadById(@PathVariable("id") Long id){
        return userService.loadById(id);
    }

    /**
     * @RequestBody  从请求体中获取数据赋值给userQuery。
     * @param query
     * @return
     */
    @PostMapping
    @ApiOperation(value = "分页查询")
//    @PreAuthorize(name = "用户管理分页查询", sn = "user:queryPage")
    public PageList<User> queryPage(@RequestBody BaseQuery query){
        try {
            return userService.queryPage(query);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<User>();
        }
    }


    /**
     * 删除
     * @DeleteMapping("/{id}") 后端接收前端参数
     * @PathVariable("id")  从资源路径上获取参数id，在赋值给Long id
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
//    @PreAuthorize(name = "用户管理删除", sn = "user:delete")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            userService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("删除失败");
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除")
//    @PreAuthorize(name = "用户管理批量删除", sn = "user:patchDelete")
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
            userService.patchDelete(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return  AjaxResult.me().setMsg("操作失败");
        }

    }




    /**
     * 添加和修改
     * @PutMapping 请求体携带参数
     * @RequestBody  从请求体中获取参数
     * @param user
     * @return
     */
    @PutMapping
    @ApiOperation(value = "添加或修改")
//    @PreAuthorize(name = "用户管理添加或修改", sn = "user:saveOrUpdate")
    public AjaxResult saveOrUpdate(@RequestBody User user){
        try {
            if (user.getId() != null){
                userService.update(user);
            } else {
                userService.save(user);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("操作失败");
        }
    }

    @PostMapping("/phoneRegister")
    @ApiOperation(value = "电话检验")
    public AjaxResult phoneRegister(@RequestBody RegisterDto dto){
        try {
            userService.phoneRegister(dto);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("网络异常,稍后重试！");
        }
    }






}
